package com.founder.gateway.system.bean;


import java.util.*;

public class ParameterMap {
    private Map<String, List<String>> param = new HashMap<>(), body = new HashMap<>();

    public Map<String, List<String>> getParam() {
        return param;
    }

    public Map<String, List<String>> getBody() {
        return body;
    }

    public void addParam(String name, String value) {
        param.computeIfAbsent(name, k -> new ArrayList<>()).add(value);
    }

    public void addBody(String name, String value) {
        body.computeIfAbsent(name, k -> new ArrayList<>()).add(value);
    }

    public void addParam(String name, List<String> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        param.computeIfAbsent(name, k -> new ArrayList<>()).addAll(list);
    }

    public void addBody(String name, List<String> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        body.computeIfAbsent(name, k -> new ArrayList<>()).addAll(list);
    }

    public void setParam(String name, String value) {
        List<String> list = new ArrayList<>();
        list.add(value);
        param.put(name, list);
    }

    public void setBody(String name, String value) {
        List<String> list = new ArrayList<>();
        list.add(value);
        body.put(name, list);
    }

    public Map<String, List<String>> getMaps() {
        Map<String, List<String>> map = new HashMap<>();
        for (Map.Entry<String, List<String>> entry : param.entrySet()) {
            map.computeIfAbsent(entry.getKey(), k -> new ArrayList<>()).addAll(entry.getValue());
        }
        for (Map.Entry<String, List<String>> entry : body.entrySet()) {
            map.computeIfAbsent(entry.getKey(), k -> new ArrayList<>()).addAll(entry.getValue());
        }
        return Collections.unmodifiableMap(map);
    }

    public Map<String, String> getMapsSingle() {
        Map<String, String> map = new HashMap<>();
        for (Map.Entry<String, List<String>> entry : getMaps().entrySet()) {
            List<String> list = entry.getValue();
            if (list != null && !list.isEmpty()) {
                map.put(entry.getKey(), list.get(0));
            }
        }
        return Collections.unmodifiableMap(map);
    }
}
